package com.example.toutiao.ui.activity;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.toutiao.R;
import com.example.toutiao.db.DatabaseHelper;

import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.net.Uri;

import java.io.File;
import java.io.IOException;
//添加商品
public class AddProductActivity extends AppCompatActivity {

    private EditText edtProductName, edtProductPrice, edtProductDescription;
    private Button btnAddProduct;
    private DatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_product);

        edtProductName = findViewById(R.id.edtProductName);
        edtProductPrice = findViewById(R.id.edtProductPrice);
        edtProductDescription = findViewById(R.id.edtProductDescription);
        btnAddProduct = findViewById(R.id.btnAddProduct);

        dbHelper = new DatabaseHelper(this);

        // 添加商品按钮点击事件
        btnAddProduct.setOnClickListener(v -> {
            String name = edtProductName.getText().toString();
            String priceStr = edtProductPrice.getText().toString();
            String description = edtProductDescription.getText().toString();

            if (name.isEmpty() || priceStr.isEmpty() || description.isEmpty()) {
                Toast.makeText(this, "商品名称、价格和描述不能为空", Toast.LENGTH_SHORT).show();
            } else {
                try {
                    double price = Double.parseDouble(priceStr);
                    boolean success = dbHelper.addProduct(name, price, description);
                    if (success) {
                        Toast.makeText(this, "商品添加成功", Toast.LENGTH_SHORT).show();
                        finish();  // 返回设置页面
                    } else {
                        Toast.makeText(this, "商品添加失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (NumberFormatException e) {
                    Toast.makeText(this, "请输入有效的价格", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    // 获取图片路径
    public String getRealPathFromURI(Uri contentUri, ContentResolver contentResolver) {
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = contentResolver.query(contentUri, proj, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {  // 确保 Cursor 有数据
            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            String filePath = cursor.getString(columnIndex);
            cursor.close();
            return filePath;
        } else {
            // 如果查询结果为空，则返回 null
            if (cursor != null) {
                cursor.close();
            }
            Log.e("getRealPathFromURI", "No data found for URI: " + contentUri.toString());
            return null;
        }
    }

}
